﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FolderManager.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the FolderManager class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.BusinessLogic
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    
    /// <summary>
    /// Defines the FolderManager class.
    /// </summary>
    public static class FolderManager
    {
        /// <summary>
        /// Initializes a new instance of the Folder class.
        /// </summary>
        /// <param name="folderId">The identity of the database table's row referencing this folder.</param>
        public static BusinessObjects.Folder GetFolder(int folderId)
        {
            return Data.Folder.Select(folderId);
        }
        
        /// <summary>
        /// Finds a folder based on its path, if it exists.
        /// </summary>
        /// <param name="path">The virtual path to search for.</param>
        /// <returns>If the folder is found, a folder is populated and returned. Returns null otherwise.</returns>
        public static BusinessObjects.Folder GetFolderByPath(string path)
        {
            return Data.Folder.Select(path);
        }

        public static List<BusinessObjects.Permission> GetPermissionsWithRoleInfo(string path)
        {
            return Data.Folder.GetPermissionsWithRoleInfo(path);
        }

        /// <summary>
        /// Inserts the Folder object into the database.
        /// </summary>
        public static BusinessObjects.Folder SaveAs(BusinessObjects.Folder folder)
        {
            return Data.Folder.Insert(folder);
        }

        /// <summary>
        /// Updates the Folder's database fields.
        /// </summary>
        public static void Save(BusinessObjects.Folder folder)
        {
            Data.Folder.Update(folder);
        }

        /// <summary>
        /// Deletes the Folder's database entry.
        /// </summary>
        public static void Delete(BusinessObjects.Folder folder)
        {
            FileInfo fi;
            List<string> filesToDelete = Data.Folder.Delete(folder);
            for (int i = 0; i < filesToDelete.Count; i++)
            {
                fi = new FileInfo(BusinessLogic.Environment.StaticFilesDirectory + filesToDelete[i]);
                fi.Delete();
            }
        }
    }
}